package dao;

import entry.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FansDao {

    // 删除粉丝信息 -- 当有用户注销时
    public void deleteFansIfLogout(int userId){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from concern where userId=? or fansId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            statement.setInt(2, userId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }
    // 根据用户 id查到其粉丝信息
    public List<User> getFansById(int userId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            List<User> users = new ArrayList<User>();
            connection = JDBCUtil.getConnection();
            String sql = "select * from user where userId in (select fansId from concern where userId=?)";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("userName"));
                user.setPhoto(resultSet.getString("photo"));
                user.setGithub(resultSet.getString("github"));

                users.add(user);
            }
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    // 新增关注
    //                    被关注者      粉丝
    public void addFans(int userId, int fansId){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into concern values (?, ?)";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            statement.setInt(2, fansId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    // 取消关注
    //                    被关注者      粉丝
    public void deleteFans(int userId, int fansId){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from concern where userId=? and fansId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            statement.setInt(2, fansId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    // 查找是否有 对应id关注其他对应id的信息
    //                    被关注者      粉丝
    public User isFans(int userId, int fansId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select * from concern where userId=? and fansId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            statement.setInt(2, fansId);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                return user;
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }
}
